package org.example.Study.filter;

import com.alibaba.fastjson.JSONObject;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * @Author : zhangbo
 * @data : 2024/4/27
 * @description :
 */
public class BodyReaderHttpServletRequestWrapper extends HttpServletRequestWrapper {
    //解密私钥
    private final String KEY="asdjhSDHFHDSIFHjhjdbk4811sdGG2fw";
    //加密密钥
    private final String L_KEY="ugfkJggiHOIUHJKbhfihjihi45w";
    private final String iv = "1234567890123456"; // 16字节IV


    // 报文
    private byte[] body;

    public String getBody(){
        return new String(body);
    }

    public String setBody(HttpServletRequest request) throws Exception {
        BufferedReader reader = request.getReader();
        StringBuilder requestBody = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            requestBody.append(line);
        }
        JSONObject jsonObject = JSONObject.parseObject(String.valueOf(requestBody));
        body = jsonObject.toString().getBytes();
        String key = AESUtil.generateAESKey();
        //加密
        String encryptAES = AESUtil.encryptAES(body.toString(), key);
        //解密
        String decryptAES = AESUtil.decryptAES(encryptAES, key);
        return decryptAES;

    }

    public BodyReaderHttpServletRequestWrapper(HttpServletRequest request) throws Exception {
        super(request);
        setBody(request);
    }

    @Override
    public BufferedReader getReader() throws IOException {
        return new BufferedReader(new InputStreamReader(getInputStream()));
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {
        final ByteArrayInputStream bais = new ByteArrayInputStream(body);
        return new ServletInputStream() {

            @Override
            public int read() throws IOException {
                return bais.read();
            }

            @Override
            public boolean isFinished() {
                return false;
            }

            @Override
            public boolean isReady() {
                return false;
            }

            @Override
            public void setReadListener(ReadListener arg0) {

            }
        };
    }


    /**
     * 解密
     */
    public  void JM(String key,String iv,String encryptedData ) {
        try {
            // 密钥和初始化向量应该是固定的，通常从安全的来源获取
//            String key = "1234567890123456"; // 16字节密钥
            // 要解密的数据，通常是Base64编码的字符串
//            String encryptedData = "encryptedDataHere";
            // 创建密钥和初始化向量的Spec
            SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
            IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes(StandardCharsets.UTF_8));
            // 解密
            byte[] decodedValue = Base64.getMimeDecoder().decode(encryptedData);
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
            byte[] decryptedBytes = cipher.doFinal(decodedValue);
            // 解密后的数据
            String decryptedData = new String(decryptedBytes, StandardCharsets.UTF_8);
            System.out.println("Decrypted Data: " + decryptedData);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
